<?php
/**
 * Fang_Service_Message
 * 
 * 作者:  姚刚 (yaogang@the9sense.com)
 * 创建时间: 2011-03-15 18:06:26
 * 修改记录: 
 * 
 * $Id: Message.php 18 2011-04-17 12:50:07Z coosuu $
 */

class Fang_Service_Message {

    const STATUS_UNPASS = 1;

    const STATUS_PASS = 2;

    const STATUS_REMOVE = 9;

    /**
     * add 
     * 
     * @param  mixed $nickname 
     * @param  mixed $content 
     * @param  integer $parentId 
     * @param  mixed $email 
     * @param  mixed $mobile 
     * @param  mixed $qq 
     * @return void
     */
    public function add($nickname, $content, $parentId = 0, $email = null, $mobile = null, $qq = null, $status = 0) {

        $data = array('me_nickname' => $nickname,
                      'me_content' => $content,
                      'me_parent_id' => $parentId,
                      'me_email' => $email,
                      'me_mobile' => $mobile,
                      'me_qq' => $qq,
                      'me_status' => $status);
        $data = array_filter($data);

        try {
            return Fang_DAO_Message::add($data);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * remove 
     * 
     * @param  mixed $meid 
     * @return void
     */
    public function remove($meid) {

        try {
            return Fang_DAO_Message::remove($meid);
        }  catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * update 
     * 
     * @param  mixed $meid 
     * @param  mixed $data 
     * @return void
     */
    public function update($meid, $data) {

        try {
            return Fang_DAO_Message::update($meid, $data);
        }  catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * listPass 
     * 
     * @param  array $pageOptions 
     * @param  array $fields 
     * @return void
     */
    public function listPass(&$pageOptions = array(), $fields = array()) {

        try {
            if ($pageOptions && !$pageOptions['totalItems']) {
                $pageOptions['totalItems'] = Fang_DAO_Message::countPass();
            }
            $pageOptions = Pager::convert($pageOptions);

            $messages = Fang_DAO_Message::listPass($pageOptions, $fields);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }

        if (!$messages || !is_array($messages)) {
            return array();
        }

        $lists = $meids = array();
        foreach ($messages as $m) {
            $meids[] = $m['meid'];
            $lists[$m['meid']] = $m;
        }
        unset($messages);

        try {
            $replies = Fang_DAO_Message::listRepliesByMeids($meids, $fields);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }

        if (!$replies || !is_array($replies)) {
            return $lists;
        }

        foreach ($replies as $k => $r) {
            if (array_key_exists($r['me_parent_id'], $lists)) {
                $lists[$r['me_parent_id']]['replies'][] = $r;
            }
        }

        return $lists;
    }

    /**
     * listUnpass 
     * 
     * @param  array $pageOptions 
     * @param  array $fields 
     * @return void
     */
    public function listUnpass(&$pageOptions = array(), $fields = array()) {

        try {
            if ($pageOptions && !$pageOptions['totalItems']) {
                $pageOptions['totalItems'] = Fang_DAO_Message::countPass();
            }
            $pageOptions = Pager::convert($pageOptions);

            return Fang_DAO_Message::listUnpass($pageOptions, $fields);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }

    }

    /**
     * pass
     * 审核 
     * 
     * @param  mixed $meid 
     * @return void
     */
    public function pass($meid) {

        $data = array('me_status' => self::STATUS_PASS);
        try {
            return Fang_DAO_Message::update($meid, $data);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }
}
